package com.saleSystem.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.saleSystem.common.ServerResponse;
import com.saleSystem.dao.UserMapper;
import com.saleSystem.pojo.User;
import com.saleSystem.service.IUserService;

/**
 * 用户服务
 * @author guawnei
 *
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService {

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserMapper usermapper;
	
	
	public ServerResponse<Boolean> userLogin(String username, String password){
		
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
			return ServerResponse.createByError("请输入用户名和密码！",false);
		}
		User user = usermapper.selectByNameAndPwd(username, password);
		if(user == null){
			return ServerResponse.createByError("用户名或密码不对！",false);
		}else{
			return ServerResponse.createBySuccess("登录成功！", true);
		}
	}
	
	
	public User getUser(String username){
		
		if(username != null && !StringUtils.isBlank(username)){
			try {
				User user = usermapper.selectByName(username);
				return user;
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("数据库查询产生异常！",e);
			}
		}
		return null;
	}
	
	
}
